<template>
  <div v-enter-helper="getList" class="app-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-row>
          <el-button type="success" @click="handleAdd">新增</el-button>
          <el-button type="danger" @click="handleDeleteAll">批量删除</el-button>
          <el-button type="danger" @click="handleFreezeAll">清除收益</el-button>
          <el-button type="primary" @click="handleExport">导出</el-button>
          <el-form
            ref="dataList2"
            :inline="true"
            :model="listQuery"
            class="demo-form-inline"
          >
            <el-form-item label="">
              <el-input
                v-model="listQuery.sociaty_no"
                clearable
                placeholder="请输入所属公会ID"
                type="text"
              ></el-input>
            </el-form-item>
            <el-form-item label="">
              <el-input
                v-model="listQuery.user_id"
                clearable
                placeholder="请输入用户ID"
                type="text"
              ></el-input>
            </el-form-item>
            <el-form-item label="">
              <el-input
                v-model="listQuery.nickname"
                clearable
                placeholder="请输入昵称"
                type="text"
              ></el-input>
            </el-form-item>

            <el-form-item>
              <el-button type="primary" @click="searchSubmit">查询</el-button>
              <el-button type="primary" @click="resetSubmit">重置</el-button>
            </el-form-item>
          </el-form>
        </el-row>
      </div>
      <div class="item">
        <el-table
          ref="dataList"
          v-loading="listLoading"
          :data="menuList"
          :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
          border
          row-key="id"
          style="width:100%;margin-bottom: 20px;"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection"></el-table-column>
          <el-table-column label="用户ID" prop="user_data.user_code"></el-table-column>
          <el-table-column label="昵称" prop="user_data.nickname"></el-table-column>
          <el-table-column label="背包收益" prop="earnings"></el-table-column>
          <!-- <el-table-column prop="price" label="价格">
            <template slot-scope="scope"> {{ scope.row.price }}金币 </template>
          </el-table-column> -->
          <el-table-column label="所属公会ID" prop="sociaty.sociaty.sociaty_no"></el-table-column>
          <el-table-column label="添加时间" prop="created_at"></el-table-column>
          <el-table-column label="操作" width="200px">
            <template slot-scope="scope">
              <el-button
                icon="el-icon-delete"
                plain
                size="mini"
                type="danger"
                @click="handleDelete(scope.row)"
              >删除
              </el-button
              >
              <el-button
                v-if="scope.row.state === 1"
                icon="el-icon-delete"
                plain
                size="mini"
                type="primary"
                @click="handleFreeze(scope.row)"
              >冻结礼物
              </el-button
              >
              <el-button
                v-if="scope.row.state === 2"
                icon="el-icon-delete"
                plain
                size="mini"
                type="info"
                @click="handleCancelFreeze(scope.row)"
              >解冻礼物
              </el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <cus-pagination
          :limit.sync="listQuery.perPage"
          :page.sync="listQuery.page"
          :total="total"
          @pagination="getList"
        />
      </div>
    </el-card>
    <!-- 删除弹框 -->
    <el-dialog
      :visible.sync="deleteDialogVisible"
      title="确定删除吗?"
      width="435px"
      @close="handleDialogClose('editRulesForm')"
    >
      <span style=""
      ><em style="font-style:normal;color:red">删除后不可恢复</em
      >，你还要继续吗？</span
      >
      <span slot="footer" class="dialog-footer">
        <el-button @click="deleteDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmDelete">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 冻结弹框 -->
    <el-dialog :visible.sync="freezeDialogVisible" title="信息">
      <span style="">确认冻结吗</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="freezeDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmFreeze">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 解冻弹框 -->
    <el-dialog :visible.sync="cancelFreezeDialogVisible" title="信息">
      <span style="">确认解冻吗</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancelFreezeDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmCancelFreeze">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 新增 -->
    <el-dialog :visible.sync="userFormDialogVisible" title="新增用户" width="30%">
      <el-form ref="userForm" :inline="false" :model="userForm" :rules="rules" label-width="120px" size="normal">
        <el-form-item label="用户ID：" prop="user_code">
          <el-input v-model="userForm.user_code" placeholder="请输入用户ID"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="userFormDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmAdd">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  addUserFreeze,
  deleteData,
  getExport,
  listData,
  listTypeData,
  postFreeze,
  postFreezeAll
} from '@/api/member/frozenIncomeList'

export default {
  data() {
    return {
      confirmBtn: false,
      userFormDialogVisible: false,
      row_state: undefined,
      row_id: undefined,
      cancelFreezeDialogVisible: false,
      freezeDialogVisible: false,
      deleteDialogVisible: false,
      listLoading: false,
      menuList: [],
      total: 10,
      listQuery: {
        page: 1,
        perPage: 10,
        sociaty_no: undefined,
        nickname: undefined,
        user_id: undefined
      },
      // 礼物列表
      typeList: [],
      // 勾选内容
      multipleSelection: [],
      // 要删除的内容的ids
      dataIds: null,
      userForm: {
        user_code: ''
      },
      rules: {
        user_code: [{ required: true, message: '请输入分类名', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getList()
    // this.getTypeList();
  },
  methods: {
    getList() {
      this.listLoading = true

      listData(this.listQuery).then(response => {
        this.menuList = response.data.list
        this.total = response.data.meta.total
        this.listLoading = false
        this.confirmBtn = false
      })
    },
    getTypeList() {
      let obj = {
        page: 1,
        perPage: 10000
      }
      listTypeData(obj).then(response => {
        response.data.list.forEach(item => {
          this.typeList.push({ value: item.id, label: item.name })
        })
      })
    },
    handleAdd() {
      this.userForm.user_code = ''
      this.userFormDialogVisible = true
    },
    confirmAdd() {
      this.$refs['userForm'].validate((valid) => {
        if (valid) {
          addUserFreeze(this.userForm).then(res => {
            if (res.code === 200) {
              this.userFormDialogVisible = false
              this.$message({
                type: 'success',
                message: '操作成功'
              })
              this.getList()
            }
          })

        } else {
          // 'error submit!!');
          return false
        }
      })
    },
    // 查询
    searchSubmit() {
      this.listQuery.page = 1
      this.getList()
    },
    // 重置
    resetSubmit() {
      this.listQuery.page = 1
      this.listQuery.perPage = 10
      this.listQuery.sociaty_no = undefined
      this.listQuery.nickname = undefined
      this.listQuery.user_id = undefined
      this.getList()
    },

    // 勾选
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    // 点击单个删除
    handleDelete(row) {
      this.dataIds = { ids: row.id.toString() }
      this.deleteDialogVisible = true
    },
    // 点击批量删除
    handleDeleteAll() {
      if (this.multipleSelection.length === 0) {
        this.$message({
          message: '请先勾选要删除的内容',
          type: 'warning'
        })
        return
      } else if (this.multipleSelection.length !== 0) {
        let ids = ''
        for (let key in this.multipleSelection) {
          ids += `${this.multipleSelection[key].id},`
        }
        ids = ids.substring(0, ids.length - 1)
        this.dataIds = { ids }
      }
      this.deleteDialogVisible = true
    },
    // 确定删除
    confirmDelete() {
      if (this.confirmBtn) return
      this.confirmBtn = true
      deleteData(this.dataIds).then(response => {
        if (response.code === 200) {
          this.getList()
          this.deleteDialogVisible = false
          this.$notify({
            type: 'success',
            message: '操作成功',
            position: 'bottom-right',
            showClose: false,
            duration: 3000
          })
        } else {
          this.confirmBtn = false
        }
      })
    },
    // 冻结礼物
    handleFreeze(row) {
      this.row_id = row.id
      this.row_state = 2
      this.freezeDialogVisible = true
    },
    // 确认冻结礼物
    confirmFreeze() {
      postFreeze(this.row_id, { state: this.row_state }).then(response => {
        if (response.code === 200) {
          this.getList()
          this.freezeDialogVisible = false
          this.$notify({
            type: 'success',
            message: '操作成功',
            position: 'bottom-right',
            showClose: false,
            duration: 3000
          })
        } else {
          this.$message({
            message: response.message,
            type: 'error'
          })
        }
      })
    },
    // 冻结礼物
    handleCancelFreeze(row) {
      this.row_id = row.id
      this.row_state = 1
      this.cancelFreezeDialogVisible = true
    },
    // 确认冻结礼物
    confirmCancelFreeze() {
      postFreeze(this.row_id, { state: this.row_state }).then(response => {
        if (response.code === 200) {
          this.getList()
          this.cancelFreezeDialogVisible = false
          this.$notify({
            type: 'success',
            message: '操作成功',
            position: 'bottom-right',
            showClose: false,
            duration: 3000
          })
        } else {
          this.$message({
            message: response.message,
            type: 'error'
          })
        }
      })
    },
    // 监听dialog关闭时的处理事件
    handleDialogClose(formName) {
      if (this.$refs[formName]) {
        this.$refs[formName].clearValidate() // 清除整个表单的校验
      }
    },

    /**批量冻结 */
    handleFreezeAll() {
      console.log(this.multipleSelection)
      if (this.multipleSelection.length === 0) {
        this.$message({
          message: '请先勾选内容',
          type: 'warning'
        })
        return
      }
      if (this.multipleSelection.length > 0) {
        let arr = []
        this.multipleSelection.forEach(item => {
          arr.push(item.id)
        })
        this.dataIds = arr //arr.join(",");
        this.$confirm('确认清除选中用户的背包收益吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          postFreezeAll(this.dataIds).then(res => {
            if (res.code === 200) {
              this.getList()
              this.cancelFreezeDialogVisible = false
              this.$notify({
                type: 'success',
                message: '操作成功',
                position: 'bottom-right',
                showClose: false,
                duration: 3000
              })
            } else {
              this.$message({
                message: res.message,
                type: 'error'
              })
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })

      }
    },
    /**导出 */
    handleExport() {

      this.$confirm('确认导出当前列表数据吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let obj = {
          sociaty_no: undefined,
          user_id: undefined,
          nickname: undefined
        }
        for (let key in obj) {
          obj[key] = this.listQuery[key]
        }
        getExport(obj).then(res => {
          if (res.code === 200) {
            window.location.href = res.data.download_url
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        })
      })
    }
  }
}
</script>

<style scoped>
[v-cloak] {
  display: none;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both;
}

.box-card {
  width: 100%;
}

.demo-form-inline {
  float: right;
}

/deep/ .el-card__header {
  padding-bottom: 0;
}
</style>
